﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ria_project.Web;

namespace ria_project
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void itemDomainDataSource_LoadedData(object sender, System.Windows.Controls.LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            itemDomainDataSource.SubmitChanges();
        }


        private void buttonAdd_Click(object sender, RoutedEventArgs e)
        {
            AddItem additemForm = new AddItem();
            additemForm.Closed += new EventHandler(additemForm_Closed);
            additemForm.Show();
        }

        void additemForm_Closed(object sender, EventArgs e)
        {
            var coll = itemDomainDataSource.DomainContext.EntityContainer.GetEntitySet<Item>();
            if (((AddItem)sender).itm != null)
            {
                coll.Add(((AddItem)sender).itm);
                itemDomainDataSource.SubmitChanges();
            }
            
        }

        private void buttonDelete_Click_1(object sender, RoutedEventArgs e)
        {
            var coll = itemDomainDataSource.DomainContext.EntityContainer.GetEntitySet<Item>();
            coll.Remove(coll.Where(i => i.ID == ((Item)itemListBox.SelectedItem).ID).First<Item>());
            itemDomainDataSource.SubmitChanges();
        }
    }
}
